<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Card\Controller;
use Common\Controller\BaseController;
use Card\Model\pvModel;
use Card\Model\PictureShareModel;
use Card\Model\CardMemberModel;
use Card\Model\UserModel;
use Card\Model\GreetingCardModel;
use Card\Model\CardIdModel;
/**
 * 统计
 *
 * @author 薛鹏
 */
class StatisticsController extends BaseController {
    public function computePV()
    {
        $data = inputJson();
        
        $pvModel = new pvModel();
        
        $date = date('Y-m-d');
        $result = $pvModel->existPV(strval($data['page_type']), $date);
        if ($result !== false) {
            if ($result !== null) {
                $pvModel->incPV($result);
            } else {
                $pvModel->addPV([
                    'page_type' => strval($data['page_type']),
                    'num' => 1,
                    'add_time' => $date
                ]);
            }
            successOutputJson2();
        } else {
            errorOutputJson2('查询数据出错'.__LINE__);
        }
    }
    
    /**
     * 记录图片分享
     */
    public function recordPictureShare()
    {
        $data = inputJson();
        
        $this->paramValidate([
            'receiver_id' => 'must',
            'card_id' => 'must',
            'type' => array('0', '1'),
        ]);
        
        $pictureShareModel = new PictureShareModel();
        $cardMemberModel = new CardMemberModel();
        $userModel = new UserModel();
        $greetingCardModel = new GreetingCardModel();
        $cardIdModel = new CardIdModel();
        
        $receiver_id = $cardMemberModel->existCardMember(['session_id' => $data['receiver_id']]);
        if ($receiver_id !== false) {
            if ($receiver_id === null)
                errorOutputJson2('接收者不存在'.__LINE__);
        } else
            errorOutputJson2('查询数据出错'.__LINE__);
        
        if (strval($data['type']) === '0') {
            $card_id = $userModel->existBusinessCard([
                'id' => $data['card_id']
            ]);
            if ($card_id !== false) {
                if ($card_id === null)
                    errorOutputJson2('名片不存在'.__LINE__);
            } else
                errorOutputJson2('查询数据出错'.__LINE__);
            
            $sender_id = $cardIdModel->getUserId($card_id);
        } elseif(strval($data['type']) === '1') {
            $card_id = $greetingCardModel->existGreetCard([
                'id' => $data['card_id']
            ]);
            if ($card_id !== false) {
                if ($card_id === null)
                    errorOutputJson2('贺卡不存在'.__LINE__);
            } else
                errorOutputJson2('查询数据出错'.__LINE__);
            
            $sender_id = $greetingCardModel->getUserId($card_id);
        }
        
        if ($sender_id == $receiver_id) {
            errorOutputJson2('不能分享给自己'.__LINE__);
        }
        
        $result = $pictureShareModel->existPictureShare(
                $sender_id, $receiver_id, $card_id, strval($data['type']));

        if ($result !== false) {
            if ($result === null) {
                $pictureShareModel->addPictureShare([
                    'sender_id' => $sender_id,
                    'receiver_id' => $receiver_id,
                    'card_id' => $card_id,
                    'type' => strval($data['type'])
                ]);
                successOutputJson2();
            } else 
                errorOutputJson2('图片分享记录已存在'.__LINE__);
        } else
            errorOutputJson2('查询数据出错'.__LINE__);
    }
}
